<?php
/**
 * Created by PhpStorm.
 * User: gwind
 * Date: 2019/1/21
 * Time: 下午 8:16
 */

namespace app\api\model;

use think\Model;
use traits\ModelTrait;


class MemberModel extends Model {
    use ModelTrait;
    protected $pk = 'id';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'meetyou_member';

    public static function getUserInfo($id) {
        $userInfo = self::where('id', '=', $id)->find();
        if (!$userInfo) exception('读取用户信息失败!');
        return $userInfo->toArray();
    }

    /**
     * 获取上级ids
     * @param $id
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getParentIds($id) {
        $user = self::where('id', $id)->field('id,parent_id,pids')->find();

        if (empty($user['pids'])) {
            $list = [];
            if (empty($user) || empty($user['parent_id'])) {
                return "";
            } else {
                $parent_id = $user['parent_id'];
                while ($agent = self::where('id', $parent_id)->field('id,parent_id')->find()) {
                    if (empty($agent)) break;
                    if (in_array($agent['id'], $list)) break;
                    $list[] = $agent['id'];
                    if (empty($agent['parent_id'])) break;
                    $parent_id = intval($agent['parent_id']);
                }
                $pids = implode(',', $list);
                self::update([['pids' => $pids], ['id' => $id]]);
            }
        } else {
            if (empty($user['parent_id'])) {
                self::update([['pids' => ''], ['id' => $id]]);
                return '';
            }
            return $user['pids'];
        }
    }


    public static function getcount() {
        return self::count();
    }

    /**
     * 查找指定条件下级数量
     * @param $id
     * @param null $where
     * @return int
     * @throws \think\Exception
     */
    public static function getAllDownCount($id, $where = null) {
        $query = self::where('FIND_IN_SET(:id,pids)', ['id' => $id]);
        if (!empty($where)) {
            $query = $query->where($where);
        }
        return intval($query->count());
    }

    /**
     * 获取直推下级数量
     * @param $id
     * @param null $where
     * @return int
     * @throws \think\Exception
     */
    public static function getDownCount($id, $where = null) {
        $query = self::where('parent_id', $id);
        if (!empty($where)) {
            $query = $query->where($where);
        }
        return intval($query->count());
    }


    public static function getDownNum($id, $level = 0) {
        $agent_1_list = self::where('parent_id', 'IN', $id)->where('level', '>=', $level)->column('id');
        $a1 = $a2 = $a3 = 0;
        if (!empty($agent_1_list)) {
            $a1 = sizeof($agent_1_list);

            $agent_2_list = self::where('parent_id', 'IN', $agent_1_list)->where('level', '>=', $level)->column('id');
            if (!empty($agent_2_list)) {
                $a2 = sizeof($agent_2_list);

                $agent_3_list = self::where('parent_id', 'IN', $agent_2_list)->where('level', '>=', $level)->column('id');
                if (!empty($agent_2_list)) {
                    $a3 = sizeof($agent_3_list);
                }
            }
        }
        return ['a1' => $a1, 'a2' => $a2, 'a3' => $a3];
    }

}